package cn.itcast.bos.utils;

import org.apache.commons.lang3.RandomStringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.net.*;

/**
 * 调用吉信通 发短信工具类
 *
 * @author itcast
 *
 */
@SuppressWarnings("all")
public class SmsUtils {
    private static String userid = "smbe";
    private static String pass = "password";

    /**
     * 调用HTTP 协议方式发送短信
     *
     * @param mobile
     * @param content
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String sendSmsByHTTP(String mobile, String content) throws UnsupportedEncodingException {
        HttpURLConnection httpconn = null;
        String result = "Error";
        StringBuilder sb = new StringBuilder();
        sb.append("http://service.winic.org:8009/sys_port/gateway/index.asp?");

        // 以下是参数
        sb.append("id=").append(URLEncoder.encode(userid, "gb2312"));
        sb.append("&pwd=").append(pass);
        sb.append("&to=").append(mobile);
        sb.append("&content=").append(URLEncoder.encode(content, "gb2312"));
        sb.append("&time=").append("");
        try {
            URL url = new URL(sb.toString());
            httpconn = (HttpURLConnection) url.openConnection();
            BufferedReader rd = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));
            result = rd.readLine();
            rd.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpconn != null) {
                httpconn.disconnect();
                httpconn = null;
            }
        }
        return result;
    }

    /**
     * 调用 WebService 协议方式发送短信
     *
     * @param mobiles
     * @param msg
     * @return
     */
    public static String sendSmsByWebService(String mobiles, String msg) {
        String result = "-12";
        try {
            Document doc;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputStream is = getSoapInputStream(userid, pass, mobiles, msg, "");
            if (is != null) {
                doc = db.parse(is);
                NodeList nl = doc.getElementsByTagName("SendMessagesResult");
                Node n = nl.item(0);
                result = n.getFirstChild().getNodeValue();
                is.close();
            }
            return result;
        } catch (Exception e) {
            System.out.print("SmsSoap.sendSms error:" + e.getMessage());
            return "-12";
        }
    }

    private static String getSoapSmssend(String userid, String pass, String mobiles, String msg, String time) {
        try {
            String soap = "";
            soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                    + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                    + "<soap:Body>" + "<SendMessages xmlns=\"http://tempuri.org/\">" + "<uid>" + userid + "</uid>"
                    + "<pwd>" + pass + "</pwd>" + "<tos>" + mobiles + "</tos>" + "<msg>" + msg + "</msg>" + "<otime>"
                    + time + "</otime>" + "</SendMessages>" + "</soap:Body>" + "</soap:Envelope>";
            return soap;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    private static InputStream getSoapInputStream(String userid, String pass, String mobiles, String msg, String time)
            throws Exception {
        URLConnection conn = null;
        InputStream is = null;
        try {
            String soap = getSoapSmssend(userid, pass, mobiles, msg, time);
            if (soap == null) {
                return null;
            }
            try {

                URL url = new URL("http://service2.winic.org:8003/Service.asmx");

                conn = url.openConnection();
                conn.setUseCaches(false);
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
                conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
                conn.setRequestProperty("HOST", "service2.winic.org");
                conn.setRequestProperty("SOAPAction", "\"http://tempuri.org/SendMessages\"");

                OutputStream os = conn.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
                osw.write(soap);
                osw.flush();
            } catch (Exception ex) {
                System.out.print("SmsSoap.openUrl error:" + ex.getMessage());
            }
            try {
                is = conn.getInputStream();
            } catch (Exception ex1) {
                System.out.print("SmsSoap.getUrl error:" + ex1.getMessage());
            }

            return is;
        } catch (Exception e) {
            System.out.print("SmsSoap.InputStream error:" + e.getMessage());
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        String randomCode = RandomStringUtils.randomNumeric(4);
        // System.out.println(sendSmsByHTTP("18612345678", randomCode));
        // System.out.println(sendSmsByHTTP("18612345678", "尊敬的用户您好，本次获取的验证码为："
        // + randomCode + ",服务电话：4006184000"));
        System.out.println(sendSmsByWebService("18612345678", "尊敬的用户您好，本次获取的验证码为："
                + randomCode + ",服务电话：4006184000"));
    }
}

